﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
using devtm.Editor.TypeSelector;


namespace devtm.AutoMapper
{
    public partial class ItemMap
    {

        // calcule de la methode de IValueConverter (Convert | ConvertBack)
        public string CastWay;

        #region Label

        private string _Label;
        internal string GetLabelValue()
        {
            string way = "->";
            return (_Label = String.Format("{0} {2} {1}", this.SourceName, this.TargetName, way));
        }

        internal void SetLabelValue(string newValue)
        {
            _Label = newValue;
        }

        #endregion


        #region TypeCast

        private CastType _TypeCast;
        internal CastType GetTypeCastValue()
        {
            return _TypeCast;
        }

        internal void SetTypeCastValue(CastType newValue)
        {
            _TypeCast = newValue;

            List<Type> lst = ContainerTypes.GetFromReference(Store, null).ToList();
            lst.AddRange(ContainerTypes.GetFromSolution<Object>(Store, null));

            using (Transaction t = Store.TransactionManager.BeginTransaction("Update Type cast"))
            {
                ///TODO remettre les type converters
                //switch (newValue)
                //{

                //    case CastType.IValueConverter:
                //        MappingHelper.FindCastIValueConverter(this, lst);
                //        break;

                //    case CastType.IConverter:
                //        MappingHelper.FindCastIConverter(this, lst);
                //        break;

                //    case CastType.TypeConverter:
                //        MappingHelper.FindCastTypeConverter(this, lst);
                //        break;

                //}
                t.Commit();
            }
        }

        #endregion

        #region Target Name

        private string _TargetName;
        internal string GetTargetNameValue()
        {
            return _TargetName;
        }

        internal void SetTargetNameValue(string newValue)
        {
            _TargetName = newValue;
            ReBuildProperties();


            //MapperShape shapeSource = (MapperShape)PresentationViewsSubject.GetPresentation(Mapper).FirstOrDefault();
            //if (shapeSource != null)
            //{
            //    var pp = shapeSource.GetCompartmentMappings();
            //    ElementListCompartmentMapping pp2 = pp[0] as ElementListCompartmentMapping;
            //}

        }

        #endregion


        #region Source Name

        private string _SourceName;
        internal string GetSourceNameValue()
        {
            return _SourceName;
        }

        internal void SetSourceNameValue(string newValue)
        {
            _SourceName = newValue;
            ReBuildProperties();
        }

        private void ReBuildProperties()
        {

            using (Transaction t = Store.TransactionManager.BeginTransaction("Update Property"))
            {
                if (string.IsNullOrEmpty(_SourceName) && !string.IsNullOrEmpty(SourceType))
                    SourceType = string.Empty;
                if (string.IsNullOrEmpty(_TargetName) && !string.IsNullOrEmpty(TargetType))
                    TargetType = string.Empty;
                t.Commit();
            }


            if (!string.IsNullOrEmpty(_SourceName) &&
                !string.IsNullOrEmpty(SourceType) &&
                !string.IsNullOrEmpty(_TargetName) &&
                !string.IsNullOrEmpty(TargetType))
            {
                using (Transaction t = Store.TransactionManager.BeginTransaction("Update Gateway"))
                {
                    MappingHelper.FindConverter(this, false);
                    Enable = !(String.IsNullOrEmpty(SourceName) || String.IsNullOrEmpty(TargetName));
                    t.Commit();
                }
            }

        }


        #endregion

        public static string FindTypeName(MemberInfo property)
        {

            Type type = property.GetTypeFrom();

            if (type.ContainsGenericParameters || (!type.IsPrimitive && !type.Assembly.Equals(typeof(string).Assembly)))
                return type.AssemblyQualifiedName;
            else
            {

                if (!string.IsNullOrEmpty(type.FullName))
                    return type.FullName;
                {

                    Type t1 = type.DeclaringType;
                    if (t1.IsGenericType)
                    { 
                    
                    }

                    return string.Empty;
                }
            }

        }


        public object Index { get; set; }

    }


}
